/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import datas.InterestMapper;
import datas.MessageMapper;
import datas.PokesMapper;
import datas.UserInterestMapper;
import datas.UserMapper;
import datas.UserRoleMapper;
import datas.WatchListMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**

 @author Marie-Claude
 */
public class MyBatisManager {

  private static SqlSessionFactory sqlSessionFactory;
  private static SqlSession session;

  public static SqlSession getSqlSession() {

    String resource = "dao/myBatis.xml";

    InputStream inputStream;
    try {
      inputStream = Resources.getResourceAsStream(resource);

      sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      sqlSessionFactory.getConfiguration().addMapper(UserMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(MessageMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(InterestMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(UserInterestMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(UserRoleMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(PokesMapper.class);
      sqlSessionFactory.getConfiguration().addMapper(WatchListMapper.class);

      session = sqlSessionFactory.openSession();

    } catch (IOException ex) {
      Logger.getLogger(MyBatisManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return session;

  }

  public static void closeConnexion() {
    session.close();
  }
}
